package com.briup.jd2511.jd2511_product_source.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.briup.jd2511.jd2511_product_source.dao.ManagerHurdlesMapper;
import com.briup.jd2511.jd2511_product_source.dao.ext.ManagerHurdlesExtMapper;
import com.briup.jd2511.jd2511_product_source.exception.ServiceException;
import com.briup.jd2511.jd2511_product_source.pojo.ManagerHurdles;
import com.briup.jd2511.jd2511_product_source.service.HurdlesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class HurdlesServiceImpl implements HurdlesService {
    @Autowired
    private ManagerHurdlesMapper managerHurdlesMapper;
    @Autowired
    private ManagerHurdlesExtMapper managerHurdlesExtMapper;

    /**
     * 分页/查询栏圈接口
     *
     * @param hName
     * @param pageName
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<ManagerHurdles> queryAllByPage(Integer pageName, Integer pageSize, String hName, Integer hMax, String fhName, String hEnable) throws ServiceException{
        PageHelper.startPage(pageName, pageSize);
        List<ManagerHurdles> managerHurdles = managerHurdlesExtMapper.queryAllHurdles(hName, hMax, fhName, hEnable);
        PageInfo<ManagerHurdles> pageInfo = new PageInfo<>(managerHurdles);
        return pageInfo;
    }

    /**
     * 查询所有的栏圈容量
     *
     * @return
     */
    @Override
    public List<Integer> findAllMax() {
        List<Integer> hMaxNum = managerHurdlesMapper.selectAllMax();
        //hMaxNum里有容量相同的，去重操作
        hMaxNum = CollUtil.newArrayList(CollUtil.distinct(hMaxNum));//去重
        return hMaxNum;
    }

    /**
     * 添加/修改栏圈
     *
     * @param managerHurdles
     */
    @Override
    public void saveOrUpdate(ManagerHurdles managerHurdles) throws ServiceException {
//        String hId = managerHurdles.getHId();
//        String hName = managerHurdles.getHName();
//        // hName不能为空
//        if(StrUtil.isBlank(hName)){
//            throw new ServiceException(ResultCode.FENCE_HOUSE_NAME_IS_BLANK);
//        }
//        List<ManagerHurdles> list = managerHurdlesMapper.selectAllByHName(hName);
//        // 判断新增还是修改：根据参数中是否有hId
//        if(StrUtil.isEmpty(hId)){ // hId为空，新增
//            // hName不能重复
//            if(CollUtil.isNotEmpty(list)){
//                throw new ServiceException(ResultCode.FENCE_HOUSE_IS_EXIST);
//            }
//            // 新增
//            // 因为没有fhId，所以需要生成一个uuid
//            managerHurdles.setHId(IdUtil.fastSimpleUUID());
//            managerHurdlesMapper.insertHurdles(managerHurdles);
//        }else { // hId不为空，修改
//            // 更新： hName和之前不一样才更新，和之前一样就不更新名字
//            ManagerHurdles existingFenceHouse = list.stream()
//                    .filter(s -> hId.equals(s.getHId()))
//                    .findFirst()
//                    .orElse(null);
//
//            if (existingFenceHouse != null && hName.equals(existingFenceHouse.getHName())) {
//                managerHurdles.setHName(null);
//                managerHurdlesMapper.updateSelective(managerFenceHouse); // 更新
//            } else {
//                managerHurdlesMapper.updateHurdles(managerHurdles);
//            }
//        }
        String hId = managerHurdles.getHId();
        if(StrUtil.isEmpty(hId)){
            // 新增
            managerHurdlesMapper.insertHurdles(managerHurdles);
        }else {
            // 修改
            managerHurdlesMapper.updateHurdles(managerHurdles);
        }
    }

    /**
     * 修改栏圈启用状态
     *
     * @param hId
     * @param hEnable
     */
    @Override
    public void updateHurdleEnable(String hId, String hEnable) throws ServiceException {
        ManagerHurdles managerHurdles = new ManagerHurdles();
        managerHurdles.setHId(hId);
        String temp = "";
        if("可用".equals(hEnable)){
            temp="禁用";
        }else if ("禁用".equals(hEnable)){
            temp="可用";
        }
        managerHurdles.setHEnable(temp);
        saveOrUpdate(managerHurdles);
    }

    /**
     * 批量禁用/启用栏圈状态
     *
     * @param list 批量修改栏圈数据
     * @throws ServiceException
     */
    @Override
    public void updateHEnableBatch(List<Map<String, String>> list) throws ServiceException {
        // 遍历list集合完成
        list.forEach(map->{
            updateHurdleEnable(map.get("hId"),map.get("hEnable"));
        });
    }

    /**
     * 查询所有启用的栏圈
     *
     * @return
     */
    @Override
    public List<ManagerHurdles> queryAllEnable() throws ServiceException {
        return managerHurdlesMapper.selectAllEnable();
    }


}
